<template>
    <!-- 组件的结构 -->
    <div class="school">
        <h2>学校名称 ：{{ name }}</h2>
        <h2>学校地址:{{ address }}</h2>
        <h2>学生名称:{{ studentName }}</h2>
    </div>
</template>

<script>
    import pubSub from 'pubsub-js'
    // 组件交互相关的代码（数据方法等）
    export default {
        name: "School",
        data() {
            return {
                name: "瑞子",
                address: "花园大道",
                studentName: ''
            };
        },
        mounted() {
            console.log("School 组件被创建了", this)
            /* this.$bus.$on('hello', (data) => {
                 console.log('我是school组件，收到了数据', data);
                 this.studentName = data;
             })*/
            this.pubId = pubSub.subscribe('hello', (functionName, data) => {
                console.log('我是school组件，收到了数据', functionName, data);
                this.studentName = data;
            })
        },
        beforeDestroy() {
            // 销毁自定义事件
            /* this.$bus.$off('hello')*/
            pubSub.unsubscribe(this.pubId);
        }
    };
</script>

<style scoped>
    .school {
        background-color: skyblue;
    }
</style>
